#include "SaveVideoTask.h"

SaveVideoTask::SaveVideoTask(char* body, int client_fd, int len,int shmid, int msgid) :BaseTask(body, client_fd, len, shmid, msgid)
{

}

void SaveVideoTask::working()
{
	SAVEVIDEO_REQ save_video = { 0 };
	//拷贝到结构体中解析
	memcpy(&save_video, body, sizeof(SAVEVIDEO_REQ));
	cout << "user id = " << save_video.user_id << endl;
	//拷贝出视频的结构体
	Video video = save_video.video;
	cout << "------------------------操作日志------------------" << endl;
	// 获取当前时间点（系统时钟）
	auto now = std::chrono::system_clock::now();
	auto in_time_t = std::chrono::system_clock::to_time_t(now);
	std::stringstream ss;
	ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");
	cout << "时间: " << ss.str();  // ctime自带换行
	cout << "功能：上传视频" << endl;
	cout << "类型：接收" << endl;
	cout << "用户id：" << save_video.user_id << endl;
	cout << "接收数据包：" << endl;
	cout << "视频名:" << video.video_name << "\n"
		<< "视频类型:" << video.video_type << "\n"
		<< "视频地址:" << video.video_path << "\n"
		<<"视频首图："<< video.video_img <<"\n"
		<<"视频总帧："<< video.video_size <<endl;

	//写入文件
	LogManager& log_fd = LogManager::getInstance();
	ofstream& fd = log_fd.getLogStream();
	fd << "时间: " << ss.str()<<"\n"  // ctime自带换行
	<< "功能：上传视频\n" 
	<< "类型：接收\n"
	<< "用户id：" << save_video.user_id << "\n"
	<< "接收数据包：\n"
	<< "视频名:" << video.video_name << "\n"
	<< "视频类型:" << video.video_type << "\n"
	<< "视频地址:" << video.video_path << "\n"
	<< "视频首图：" << video.video_img << "\n"
	<< "视频总帧：" << video.video_size << "\n"
	<< "--------------------------------------------" << "\n\n";
	// 4. 强制刷新缓冲区（确保日志立即写入文件）
	fd.flush();
	//执行插入语句
	if (model->insetVideo(video, save_video.user_id))
	{
		send(Success, "上传视频：上传成功", save_video.user_id);
	}
	else
	{
		send(Failure, "上传视频：上传失败", save_video.user_id);
	}
}

void SaveVideoTask::send(int statu, string message,int user_id)
{
	ShmControl* shm = (ShmControl*)shmat(shmid, NULL, 0);
	int index = -1;//空闲下标
	// 寻找下一个空闲块（状态0）
	for (int i = 0; i < MEMORYNUMBER; i++) {
		int status = shm->blocks[i].status;
		if (status == 0) { // 空闲
			index = i;
			break;
		}
	}
	HEAD head = { 0 };
	RESP resp = { 0 };//响应
	char send[MEMORYSIZE] = { 0 };
	strcpy(resp.message, message.c_str());
	resp.status_code = statu;
	//写头
	head.service_type = SAVEVIDEO;
	head.checksum = 100;
	head.data_len = sizeof(RESP);
	//头体结合
	memcpy(send, &head, sizeof(HEAD));
	memcpy(send + sizeof(HEAD), &resp, sizeof(RESP));
	//写入共享内存
	sem_p(sem_shm, 0);//获取共享内存锁
	ShmBlock* block = &shm->blocks[index];
	memcpy(block->data, send, sizeof(block->data));//考入共享内存
	block->status = 1;                   // 标记为已写
	shmdt(shm);
	sem_v(sem_shm, 0);//释放共享内存锁
	//写入消息队列
	MSG msg = { 0 };
	msg.client_fd = client_fd;
	msg.index = index;
	msg.mtype = 1;
	msgsnd(msgid, &msg, sizeof(MSG), 0);
	cout << "------------------------操作日志------------------" << endl;
	// 获取当前时间点（系统时钟）
	auto now = std::chrono::system_clock::now();
	auto in_time_t = std::chrono::system_clock::to_time_t(now);
	std::stringstream ss;
	ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %H:%M:%S");

	cout << "时间: " << ss.str();  // ctime自带换行
	cout << "功能：上传视频" << endl;
	cout << "类型：发送" << endl;
	cout << "用户id：" << user_id << endl;
	cout << "发送数据包：" << endl;
	cout << "状态:" << statu << "\n"
		<< "信息:" << message << endl;

	//写入文件
	LogManager& log_fd = LogManager::getInstance();
	ofstream& fd = log_fd.getLogStream();
	fd << "时间: " << ss.str()<<"\n"
	<< "功能：上传视频\n"
	<< "类型：发送\n" 
	<< "用户id：" << user_id <<"\n"
	<< "发送数据包：\n"
	<< "状态:" << statu << "\n"
	<< "信息:" << message << "\n"
	<< "--------------------------------------------" << "\n\n";
	// 4. 强制刷新缓冲区（确保日志立即写入文件）
	fd.flush();
}
